﻿using System;
using System.Collections.Generic;

namespace BusinnesLogic.ServerObjects
{
    public abstract class Table
    {
        #region Ctor

        /// <summary>
        /// Initializes Table class
        /// </summary>
        public Table(string tableName)
        {
            this.Name = ParseTableName(tableName);
        }

        public Table(string database, string tableName)
        {
            this.Name = ParseTableName(tableName);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates a table from the database.
        /// </summary>
        public virtual void Create()
        { }

        /// <summary>
        /// Removes the table from the database.
        /// </summary>
        public virtual void Drop()
        { }

        /// <summary>
        /// Updates a table from the database.
        /// </summary>
        public virtual void Alter()
        { }

        /// <summary>
        /// Renames the table.
        /// </summary>
        /// <param name="newTableName">New table name</param>
        public virtual void Rename(string newTableName)
        { }

        public string ParseTableName(string tableName)
        {
            string name = string.Empty;

            // Remove [] branches
            foreach (char s in tableName)
            {
                if (s != '[' && s != ']')
                    name += s;
            }

            // Remove dbo before the name
            if (name.StartsWith("dbo."))
                return name.Remove(0, 4);
            else
                return name;
        }

        #endregion

        #region Public Properties

        public string Name
        { get; set; }

        /// <summary>
        /// Gets the date and time when the table was created.
        /// </summary>
        public DateTime CreateDate
        { get; internal set; }

        /// <summary>
        /// Gets the storage space used by the rows of the referenced table in kilobytes.
        /// </summary>
        public double DataSpaceUsed
        { get; internal set; }
       
        /// <summary>
        /// Gets the date and time when the table was last modified.
        /// </summary>
        public DateTime DateLastModified
        { get; internal set; }

        /// <summary>
        /// Return all the foreign keys defined on the table.
        /// </summary>
        public string ForeignKeys
        { get; internal set; }

        /// <summary>
        /// Gets the ID value that uniquely identifies the table.
        /// </summary>
        public int ID
        { get; internal set; }

        /// <summary>
        /// Gets the number of rows in the table.
        /// </summary>
        public long RowCount
        { get; internal set; }

        public abstract List<Column> Columns
        { get; }

        public abstract Column this[int index]
        {
            get;
            set;
        }

        #endregion
    }
}
